#!/bin/bash

# QEMU K8s网络状态检查脚本

echo "=== QEMU K8s网络状态 ==="
echo

# 检查网桥状态
echo "网桥状态:"
if ip link show "k8s-br0" >/dev/null 2>&1; then
    echo "✓ 网桥 k8s-br0 已创建"
    ip addr show "k8s-br0" | grep inet
else
    echo "✗ 网桥 k8s-br0 不存在"
fi
echo

# 检查IP转发
echo "IP转发状态:"
if [ "$(cat /proc/sys/net/ipv4/ip_forward)" = "1" ]; then
    echo "✓ IP转发已启用"
else
    echo "✗ IP转发未启用"
fi
echo

# 检查iptables规则
echo "NAT规则状态:"
DEFAULT_INTERFACE=$(ip route | grep default | awk '{print $5}' | head -n1)
if [ -n "$DEFAULT_INTERFACE" ]; then
    echo "默认接口: $DEFAULT_INTERFACE"
    if iptables -t nat -C POSTROUTING -s "192.168.100.0/24" -o "$DEFAULT_INTERFACE" -j MASQUERADE 2>/dev/null; then
        echo "✓ NAT规则已配置"
    else
        echo "✗ NAT规则未配置"
    fi
else
    echo "✗ 无法检测默认网络接口"
fi
echo

# 检查systemd服务
echo "服务状态:"
if systemctl is-enabled qemu-k8s-network.service >/dev/null 2>&1; then
    echo "✓ qemu-k8s-network.service 已启用"
else
    echo "✗ qemu-k8s-network.service 未启用"
fi
